/*
 * start.scala
 */

package acorn.jash.commands

import java.io.{IOException,File}

import acorn.jash.{Cmd,Shell,Process}

final class clear extends Cmd {
  override def exec = Runtime.getRuntime.exec("cmd.exe /c cls")
}


/**
 * Start a new Windows cmd window and run the command.
 * Note: the process started this way doesn't support pipe.
 * The message in the pipe just gets discarded.
 * <p/>
 * We have to run some processes, such as 'less', this way,
 * as they take user's input which couldn't be accomplished
 * smoothly in the same window as Jash due to Java's limitation.
 */
class start extends Cmd {
  final override def exec {
    try {
      (new Process("cmd.exe /c start " + cmd)).run()
    }
    catch {
      case e:IOException => Cmd.throwCNF(name)
    }
  }

  final def cmd = name + argstr

  def argstr = (""/:args)(_+" "+_)

  def name = "start"
}


/**
 * Start 'less' program to view a file. Pipe is not supported
 * as described above.
 */
class less extends start {
  override def argstr = {
    if (args == Nil)
      throwJE("Missing filename (\"less --help\" for help)")

    /*
    val n = args map(arg => if(arg == "--help")arg
                            else (Shell.wd changeTo arg).realpath)

    val m = n.filter(arg => if (arg == "--help") true
                            else (new File(arg).exists))
    */
    
    (""/:args)(_+" "+_)
  }

  override def name = "less"
}

